import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { SSkillMsg } from "../../../Message/Game/SSkillMsg";
import { CopyConfig } from "../resource/CopyConfig";
import { SkillInfo } from "../model/SkillInfo";
import { SkillConfig } from "../resource/SkillConfig";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { DataEvent } from "../events/DataEvent";
import { KeyBoardManager } from "../../../com/gengine/keyBoard/KeyBoardManager";
import { KeyEvent } from "../../../com/gengine/keyBoard/KeyEvent";
import { KeyCode } from "../../../com/gengine/keyBoard/KeyCode";
import { SpecialSkillPanel } from "../view/bossField/SpecialSkillPanel";
//class SpecialSkillController
    
    export  class SpecialSkillController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.SpaceSkillUpdate, this.onSpaceSkillUpdate);
            Dispatcher.addEventListener(EventName.Skill_CoolDown, this.useSkillItem);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.SpecialSkillDelaySetSkillPanelsPlace, this.delaySetSkillPanelPlacesHandler);
            return;
        }

        /* internal  */onSpaceSkillUpdate(arg1: SSkillMsg): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=null;
            var loc7=false;
            if (arg1.skills.length > 0) 
            {
                loc1 = [];
                loc5 = 0;
                while (loc5 < arg1.skills.length) 
                {
                    loc6 = CopyConfig.instance.getHeroBattleDic(arg1.skills[loc5].code);
                    if (!(cache.copy.isHeroBattleCopy() && loc6 == null)) 
                    {
                        loc2 = new SkillInfo();
                        loc4 = arg1.skills[loc5];
                        loc3 = SkillConfig.instance.getInfoByName(loc4.code);
                        loc2.tSkill = loc3;
                        loc2.cdDt = loc4.cdDt;
                        loc2.learned = true;
                        loc1.push(loc2);
                    }
                    ++loc5;
                }
                cache.skill.skillSpecialMaps = loc1;
                cache.skill.updateSkillCdTime(loc1);
                this.skillPanel.setSkillListData(loc1);
                this.skillPanel.show();
                if (this._delaySetSkillPanelPlace != null) 
                {
                    this._skillPanel.show(this._delaySetSkillPanelPlace.x, this._delaySetSkillPanelPlace.y);
                    this._delaySetSkillPanelPlace = null;
                }
                if (GameMapUtil.curMapState.isBossField || GameMapUtil.curMapState.isCrossDefenceCopy || GameMapUtil.curMapState.isRobCityCopyThree || GameMapUtil.curMapState.isCrossBossField || cache.copy.isHeroBattleCopy()) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShowOrHideShortcut, false));
                }
                KeyBoardManager.instance.addEventListener(KeyEvent.KEY_DOWN, this.keyDownHandler);
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowPetAvatar, false));
            }
            else 
            {
                loc7 = true;
                if (this._skillPanel) 
                {
                    this._skillPanel.hide();
                }
                if (cache.copy.isHeroBattleCopy()) 
                {
                    loc7 = false;
                }
                cache.skill.skillSpecialMaps = [];
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowOrHideShortcut, loc7));
                KeyBoardManager.instance.removeEventListener(KeyEvent.KEY_DOWN, this.keyDownHandler);
            }
            return;
        }

        /* internal  */keyDownHandler(arg1: KeyEvent): void
        {
            var loc1=arg1.keyEvent;
            if (loc1.keyCode < KeyCode.N0 || loc1.keyCode > KeyCode.N9) 
            {
                return;
            }
            var loc2=0;
            var loc3=loc1.keyCode;
            switch (loc3) 
            {
                case KeyCode.N1:
                {
                    loc2 = 0;
                    break;
                }
                case KeyCode.N2:
                {
                    loc2 = 1;
                    break;
                }
                case KeyCode.N3:
                {
                    loc2 = 2;
                    break;
                }
                case KeyCode.N4:
                {
                    loc2 = 3;
                    break;
                }
                case KeyCode.N5:
                {
                    loc2 = 4;
                    break;
                }
                case KeyCode.N6:
                {
                    loc2 = 5;
                    break;
                }
                case KeyCode.N7:
                {
                    loc2 = 6;
                    break;
                }
                case KeyCode.N8:
                {
                    loc2 = 7;
                    break;
                }
                case KeyCode.N9:
                {
                    loc2 = 8;
                    break;
                }
                case KeyCode.N0:
                {
                    loc2 = 9;
                    break;
                }
            }
            if (this._skillPanel) 
            {
                this._skillPanel.keyActive(loc2);
            }
            return;
        }

        /* internal  */useSkillItem(arg1: DataEvent): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=false;
            if (this._skillPanel) 
            {
                loc1 = int(arg1.data);
                loc2 = cache.skill.getSkillInfoById(loc1);
                if (loc2) 
                {
                    loc3 = loc2.leadTime > 0 || loc2.useTime > 0 ? false : true;
                    this._skillPanel.useSkillItem(loc2, true, loc3);
                }
            }
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (!(GameMapUtil.curMapState.isBossField || GameMapUtil.curMapState.isCrossDefenceCopy || cache.copy.isInEatBeansCopy() || GameMapUtil.curMapState.isCountryMoney || GameMapUtil.curMapState.isRobFlag || cache.copy.isHeroBattleCopy())) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowOrHideShortcut, true));
                if (this._skillPanel) 
                {
                    this._skillPanel.hide();
                }
            }
            if (GameMapUtil.curMapState.isCrossDefenceCopy) 
            {
                if (this._skillPanel) 
                {
                    this._skillPanel.showMode = SpecialSkillPanel.ShowMode_CrossTower;
                }
            }
            else if (this._skillPanel) 
            {
                this._skillPanel.showMode = SpecialSkillPanel.ShowMode_Normal;
            }
            return;
        }

        /* internal  */delaySetSkillPanelPlacesHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Object /* flash.geom.Point */;
            if (loc1 == null) 
            {
                return;
            }
            this._delaySetSkillPanelPlace = loc1;
            if (this._skillPanel == null) 
            {
                return;
            }
            this._skillPanel.show(this._delaySetSkillPanelPlace.x, this._delaySetSkillPanelPlace.y);
            this._delaySetSkillPanelPlace = null;
            return;
        }

        public get skillPanel(): SpecialSkillPanel
        {
            this._skillPanel = SpecialSkillPanel.instance;
            return SpecialSkillPanel.instance;
        }

        private /* var */_delaySetSkillPanelPlace: Object /* flash.geom.Point */;

        private /* var */_skillPanel: SpecialSkillPanel;
    }
